package ouisseuk.gui;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import ouisseuk.model.Image;
import ouisseuk.model.Release;
import ouisseuk.model.Track;
import ouisseuk.tools.Discogs;

import com.flexycore.iphone.coregraphics.CGRect;
import com.flexycore.iphone.coregraphics.CGSize;
import com.flexycore.iphone.uikit.UIColor;
import com.flexycore.iphone.uikit.UILabel;
import com.flexycore.iphone.uikit.UINavigationController;
import com.flexycore.iphone.uikit.UIScrollView;
import com.flexycore.iphone.uikit.UITextAlignment;
import com.flexycore.iphone.uikit.UIWebView;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
public class ReleaseView extends UIScrollView {

	public ReleaseView(String uri, final UINavigationController n) {
		super();
		init();
		setBackgroundColor(UIColor.clearColor());

		Release release = null;
		try {
			release = Discogs.getRelease(uri);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		int totalY = 0;

		if (!release.getImages().isEmpty()) {
			Image image = release.getImages().get(0);
			String smallUri = image.getSmallUri();
			CGRect webFrame = new CGRect();
			webFrame.init(85, 10, 150, 150);

			totalY += 160;

			UIWebView aWebView = new UIWebView();
			aWebView.initWithFrame(webFrame);
			aWebView.setBackgroundColor(UIColor.blackColor());
			aWebView.setScalesPageToFit(true);

			String html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">" + "<head>" + "<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />" + "<meta name=\"viewport\" content=\"width = 150\">" + "</head>" + "<body style=\"background-color:black\">" + "<img src=" + smallUri + " />" + "</body>" + "</html>";
			aWebView.loadHTMLStringBaseURL(html, "http://www.discogs.com/");

			// aWebView.setAutoresizesSubviews(true);

			addSubview(aWebView);
		}

		/*
		 * Artists
		 */

		// TODO: more artists
		UILabel matureProperty = new UILabel();
		matureProperty.initWithFrame(CGRect.CGRectMake(0, totalY + 20, 320, 30)); // 180
		totalY += 50;
		matureProperty.setBackgroundColor(UIColor.clearColor());
		matureProperty.setTextColor(UIColor.whiteColor());
		// bug ?
		// matureProperty.setText(release.getArtists().get(0));
		String artistName = release.getArtists().get(0);
		matureProperty.setText(artistName);
		matureProperty.setTextAlignment(UITextAlignment.UITextAlignmentCenter);
		addSubview(matureProperty);

		/*
		 * Title
		 */

		UILabel couleurProperty = new UILabel();
		couleurProperty.initWithFrame(CGRect.CGRectMake(0, totalY, 320, 30)); // 210
		totalY += 30;
		couleurProperty.setBackgroundColor(UIColor.clearColor());
		couleurProperty.setTextColor(UIColor.lightGrayColor());
		couleurProperty.setText(release.getTitle());
		couleurProperty.setTextAlignment(UITextAlignment.UITextAlignmentCenter);
		addSubview(couleurProperty);

		{
			/*
			 * Release Date
			 */
			UILabel dateLabel = new UILabel();
			dateLabel.initWithFrame(CGRect.CGRectMake(30, totalY, 120, 30)); // 240
			dateLabel.setBackgroundColor(UIColor.clearColor());
			dateLabel.setTextColor(UIColor.darkGrayColor());
			dateLabel.setText("Release Date: ");
			addSubview(dateLabel);

			UILabel dateView = new UILabel();
			dateView.initWithFrame(CGRect.CGRectMake(142, totalY, 180, 30)); // 240
			dateView.setBackgroundColor(UIColor.clearColor());
			dateView.setTextColor(UIColor.lightGrayColor());
			dateView.setText(release.getReleased());
			addSubview(dateView);

			totalY += 30;
		}

		{
			/*
			 * Labels
			 */
			UILabel labelsLabel = new UILabel();
			labelsLabel.initWithFrame(CGRect.CGRectMake(30, totalY, 120, 30)); // 270
			labelsLabel.setBackgroundColor(UIColor.clearColor());
			labelsLabel.setTextColor(UIColor.darkGrayColor());
			labelsLabel.setText("Labels: ");
			addSubview(labelsLabel);

			UILabel labelView = new UILabel();
			labelView.initWithFrame(CGRect.CGRectMake(89, totalY, 220, 30)); // 270
			labelView.setBackgroundColor(UIColor.clearColor());
			labelView.setTextColor(UIColor.lightGrayColor());
			labelView.setText(release.getLabelsAsString());
			addSubview(labelView);

			totalY += 30;
		}

		String country = release.getCountry();
		if (country != null) {
			/*
			 * Country
			 */
			UILabel labelsLabel = new UILabel();
			labelsLabel.initWithFrame(CGRect.CGRectMake(30, totalY, 120, 30)); // 270
			labelsLabel.setBackgroundColor(UIColor.clearColor());
			labelsLabel.setTextColor(UIColor.darkGrayColor());
			labelsLabel.setText("Country: ");
			addSubview(labelsLabel);

			UILabel labelView = new UILabel();
			labelView.initWithFrame(CGRect.CGRectMake(97, totalY, 220, 30)); // 270
			labelView.setBackgroundColor(UIColor.clearColor());
			labelView.setTextColor(UIColor.lightGrayColor());
			labelView.setText(country);
			// remarqueView.setFont(UIFont.fontWithNameSize("Arial", 14));
			// labelView.setEditable(false);
			addSubview(labelView);

			totalY += 30;
		}

		totalY += 20;

		/*
		 * TrackList
		 */
		List<Track> trackList = release.getTrackList();
		Iterator<Track> it = trackList.iterator();
		while (it.hasNext()) {
			Track track = it.next();

			String trackText = " - ";
			if (track.getPosition() != null)
				trackText = track.getPosition() + trackText;
			if (track.getTitle() != null)
				trackText += track.getTitle();
			if (track.getDuration() != null)
				trackText += " (" + track.getDuration() + ")";

			UILabel labelsLabel = new UILabel();
			labelsLabel.initWithFrame(CGRect.CGRectMake(30, totalY, 260, 30)); // 270
			labelsLabel.setBackgroundColor(UIColor.clearColor());
			labelsLabel.setTextColor(UIColor.lightGrayColor());
			labelsLabel.setText(trackText);
			addSubview(labelsLabel);

			totalY += 30;
		}

		totalY += 20;

		CGSize size = new CGSize();
		size.setHeight(totalY);
		size.setWidth(320);
		setContentSize(size);

	}
}
